import Duck from 'extensible-duck'
import produce from 'immer'

export default new Duck({
  namespace: 'label',
  store: 'user',
  types: ['LOAD', 'RECIVE', 'REMOVE'],
  initialState: {},
  reducer: produce((draft, action, duck) => {
    switch (action.type) {
      case duck.types.REMOVE:
        draft.user = undefined
        break
      case duck.types.RECIVE:
        draft.user = action.user
        break
      default:
        break
    }
  }),
  selectors: {
    user: state => state.user.user,
    isUserLoad: state => state.user.user !== undefined,
  },
  creators: duck => ({
    actionLoadUser: () => ({ type: duck.types.LOAD }),
    actionReciveUser: user => ({ type: duck.types.RECIVE, user }),
    actionRemoveUser: () => ({ type: duck.types.REMOVE }),
  }),
})
